/*
 *
 * Copyright (c) 2021
 * User:Dybala
 * File:UserService.java
 * Date:2021/11/28 20:51:28
 */

package com.service;

import com.dao.TaskDao;
import com.dao.UserDao;
import com.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    redisService redisService;
    @Autowired
    TaskDao taskdao;

    @Autowired
    UserDao userDao;
    /**
     * 修改密码
     * @param code 验证码
     * @param newPwd 新密码
     * @return
     */
    public Message resetPwd(String code,String newPwd,String email){
        Message msg=new Message();
        String key=email+"set";
        if(newPwd==null||newPwd.length()<6){
            msg.setFlag(400);
            msg.setMsg("密码太短");
            return msg;
        }
        String str = redisService.get(key);
        if(!code.equals(str)){
            msg.setFlag(400);
            msg.setMsg("验证码错误");
            return msg;
        }else{
            int num = userDao.updatePwd(email, newPwd);
            if(num!=1){
                msg.setFlag(400);
                msg.setMsg("发生未知错误");
            }else{
                msg.setFlag(200);
                msg.setMsg("修改成功");
            }
        }
        return msg;
    }

    /**
     * 修改名字
     * @param email 修改者邮箱
     * @param newName 新名字
     * @return
     */
    public Message resetUserName(String email,String newName){
        Message msg=new Message();
        if(newName==null||newName.length()==0){
            msg.setFlag(400);
            msg.setMsg("用户名长度太短了");
            return msg;
        }
        User users = userDao.queryByUserName(newName);
        //已经被占用了
        if(users!=null&&!users.getEmail().equals(email)){
            msg.setFlag(400);
            msg.setMsg("username已被占用，请换个名称试试");
        }else{
            userDao.updateUserName(email,newName);
            msg.setFlag(200);
            msg.setMsg("username修改成功");
        }
        return msg;
    }

    /**
     * 重置手机号
     * @param phone 手机号
     * @return
     */
    public Message resetPhone(String phone,String email){
        Message msg=new Message();
        if(phone==null||phone.length()<5){
            msg.setFlag(400);
            msg.setMsg("联系方式长度太短");
            return msg;
        }
        for(char c:phone.toCharArray()){
            if(c<'0'||c>'9'){
                msg.setFlag(400);
                msg.setMsg("联系方式不能带有除数字以外的特殊符号");
                return msg;
            }
        }
        int num = userDao.updatePhone(phone, email);
        if(num!=1){
            msg.setFlag(400);
            msg.setMsg("设置失败，请重试");
        }else{
            msg.setFlag(200);
            msg.setMsg("联系方式设置成功");
        }
        return msg;
    }

    public User queryDetails(String email){
        User user = userDao.queryByEmail(email);
        return user;
    }

    public Message reportToAdmin(Report report){
        User user = userDao.queryByEmail(report.getLanucher());
        if("active".equals(user.getStatus())){
            Message msg=new Message();
            msg.setFlag(200);
            msg.setMsg("账号未被冻结");
            return msg;
        }
        Report temp = userDao.getReportTypeTwo(report.getLanucher());
        Message msg=new Message();
        if(temp!=null){
            msg.setFlag(200);
            msg.setMsg("宁已经提交过申诉，现在已经更新");
            report.setId(temp.getId());
            userDao.updateReport(report);
            return msg;
        }else{
            taskdao.reportToAdmin(report);
            msg.setFlag(200);
            msg.setMsg("接收到申诉");
        }
        return msg;
    }

    public List<Comment> queryComment(String email){
        List<Comment> comments = userDao.queryComment(email);
        return comments;
    }

    public Map<String,Object> initData(String email) {
        //首先获取用户名
        User user = userDao.queryByEmail(email);
        Map<String ,Object> map=new HashMap<>();
        map.put("name",user.getUsername());
        //获取发布的任务的数量
        int count = taskdao.countMyTasks(email);
        map.put("count",count);
        //获取正在处理的业务
        List<PubTask> tasks = taskdao.queryPubByEmail(email);
        int num=tasks.size();
        map.put("working",num);
        //获取平均得分：
        List<Comment> comments = queryComment(email);
        if(comments.isEmpty()){
            map.put("avl",0);
        }else{
            double avl;
            int good=0;
            for(Comment t:comments){
                good+=t.getScore();
            }
            avl=(double)good/(double)comments.size();
//        System.out.println(avl);
            avl = (double) Math.round(avl * 100) / 100;
            map.put("avl",avl);
        }

        List<PubTask> pubTasks = taskdao.queryMyWorker(email);
        map.put("worker",pubTasks.size());
        return map;
    }

    public List<Notice> readNotice() {
        List<Notice> notices = userDao.readNotice();
        return notices;
    }
}
